//
//  UIBarButtonItem+Extension.swift
//  ZHWeibo
//
//  Created by user on 15/12/5.
//  Copyright © 2015年 heima. All rights reserved.
//

import UIKit

//设置文字的默认大小
let titleFontSize: CGFloat = 14.0
//设置文字的默认颜色
/// ???????这个white有什么特殊作用
let titleColor: UIColor = UIColor(white: 80 /  255, alpha: 1)

extension UIBarButtonItem{
    //设置NavItem的样式
    /**
      * string后面紧跟“?”
      * 为什么便利构造函数不提供返回值？？？？？？？？？？？？？  
    */
    convenience init(imageName: String? = nil, title: String? = nil, target: AnyObject?, action: Selector){
        //只有便利构造函数中才可以使用self.init()
        self.init()
        let Btn: UIButton = UIButton()
        //添加点击事件
        /**
        *  ???????搞清内部参数和外部参数的区别
        */
        Btn.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        
        if let img = imageName {
            Btn.setImage(UIImage(named: img), forState: UIControlState.Normal)
            Btn.setImage(UIImage(named: "\(img)_highlighted") , forState: UIControlState.Highlighted)
        }
        if let text = title {
            Btn.setTitle(text, forState: UIControlState.Normal)
            Btn.setTitleColor(titleColor, forState: UIControlState.Normal)
            Btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Highlighted)
            Btn.titleLabel?.font = UIFont.systemFontOfSize(titleFontSize)
        }
        //设置Btn大小
        Btn.sizeToFit()
        //设置customView为Btn
        customView = Btn
    }
}
